from django.db import models
from django.utils import timezone

import uuid


# Create your models here.

class User(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(unique=True, max_length=200)
    password = models.CharField(max_length=128, help_text='登录密码(不可逆加密)')
    nickname = models.CharField(max_length=200, help_text='用户昵称')
    mail = models.EmailField(blank=True, null=True)
    register_time = models.DateTimeField(default=timezone.datetime.now)
    last_login = models.DateTimeField(default=timezone.datetime.now)
    available = models.BooleanField(default=True, help_text='账户是否可用')
    account_platforms = models.ManyToManyField('AccountPlatform', blank=True)
    accounts = models.ManyToManyField("Account", blank=True)

    def __str__(self):
        return self.name


class Session(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4().hex)
    user = models.ForeignKey('User', on_delete=models.CASCADE)
    create_time = models.DateTimeField(default=timezone.now)
    last_visit_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.id


class AccountPlatform(models.Model):
    """账户所在平台，例如有多个QQ号，这些QQ号都属于腾讯这个平台"""
    name = models.CharField(primary_key=True, max_length=200, help_text='账户平台名称')
    description = models.TextField(blank=True, null=True, help_text='附加说明')

    def __str__(self):
        return self.name


class Account(models.Model):
    id = models.AutoField(primary_key=True)
    platform = models.ForeignKey('AccountPlatform', blank=True, null=True, on_delete=models.SET_NULL)
    name = models.CharField(max_length=200, default='自定义的腾讯企业邮箱')
    account = models.CharField(max_length=200, default=uuid.uuid4().hex[26:])
    password = models.CharField(max_length=200, default=uuid.uuid4().hex)
    notes = models.TextField(blank=True)
    create_time = models.DateTimeField(default=timezone.now)
    extra_info = models.ManyToManyField("AccountExtraInfo", blank=True)

    def __str__(self):
        return self.name


class AccountExtraInfo(models.Model):
    name = models.CharField(max_length=200)
    value = models.TextField()

    def __str__(self):
        return self.name
